package com.link.play.fit.ai.ui.weightcontrol.ac

import android.graphics.Color
import android.os.Bundle
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.databinding.AcIntermittentFastingBinding
import com.link.play.fit.ai.ui.weightcontrol.IntermittentFastingFg
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle

class IntermittentFastingAc : BaseActivity<AcIntermittentFastingBinding>(AcIntermittentFastingBinding::inflate) {

    private val page0 by lazy {
        IntermittentFastingFg().apply {
            page = 0
            pageIndex = 0
        }
    }

    private val page1 by lazy {
        IntermittentFastingFg().apply {
            page = 1
            pageIndex = 1
        }
    }

    private val page2 by lazy {
        IntermittentFastingFg().apply {
            page = 2
            pageIndex = 2
        }
    }
    var currentIndex = 0
    private val fragments: MutableList<Fragment> by lazy {
        mutableListOf(page0, page1, page2)
    }


    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.clTop.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewPager2()

        onBackPressedDispatcher.addCallback {
            when (currentIndex) {
                0 -> {
                    finish()
                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
                }

                else -> {
                    binding.viewPager2.setCurrentItem(currentIndex - 1, true)
                }
            }

        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvNext.setOnClickListener {
            if (currentIndex == 2) {
                goToAc(WeightControlQuestAc::class.java)
                overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
            } else {
                binding.viewPager2.setCurrentItem(currentIndex + 1, true)
            }

        }
    }

    private fun initViewPager2() {

        binding.indicatorView.apply {

            setSliderColor(
                Color.parseColor("#FFD9DADC"),
                ContextCompat.getColor(this@IntermittentFastingAc, R.color.colorPrimary)
            )
            setSliderWidth(resources.getDimension(R.dimen.dp_10))
            setSliderHeight(resources.getDimension(R.dimen.dp_10))
            setSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorStyle(IndicatorStyle.CIRCLE)
            setPageSize(fragments.size)
            notifyDataChanged()

        }

        binding.viewPager2.run {
            isUserInputEnabled = true
            adapter = object : FragmentStateAdapter(this@IntermittentFastingAc) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                binding.indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                currentIndex = position
                binding.indicatorView.onPageSelected(position)
                changeUi()
            }
        })
    }

    private fun changeUi() {
        when (currentIndex) {

        }
    }


}